package com.cloudptt.wp;


import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.func.message.NotifyListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.ui.HomeActivity;
import com.cloudptt.wp.ui.call.group.GroupCallVideoActivity;
import com.cloudptt.wp.ui.call.single.CallActivity;
import com.cloudptt.wp.ui.call.single.fv.FloatWindowService;
import com.mcptt.http.operator.IHttpAddrOpCb;


import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;


/**
 * 继承Application
 * 
 * @author admin
 * 
 */
public class SysApplication extends Application implements NotifyListener
{
	public static final String APPID = "appid=1442820561";//0x55ffb1d1
	public static String THIS_FILE = "SysApplication";
	public static String APP_KEY = "ptt/wuping";
	
	private Product product = null;
	private IHttpAddrOpCb addrUptListener = null;
	private AppCloseReceiver appCloseReceiver;
	private TelephonyManager tm = null;
	private AppPhoneStateListener phoneStateListener = null;
	@Override
	public void onCreate()
	{
		super.onCreate();
		Log.d(THIS_FILE,"onCreate");
//		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//		phoneStateListener = new AppPhoneStateListener();
//		tm.listen(phoneStateListener,PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_CELL_INFO);
		appCloseReceiver = new AppCloseReceiver(this);
		IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
		registerReceiver(appCloseReceiver, filter);
//		AndroidRemoteDebugger.init(this);
	}
	

	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
		Log.d(THIS_FILE,"onLowMemory");
	}
	public void onTrimMemory(int level) 
	{
		super.onTrimMemory(level);
		Log.d(THIS_FILE,"onTrimMemory level = " + level);
	};


	@Override
	public void onTerminate()
	{
		Log.d(THIS_FILE,  "onTerminate");
		if(tm != null
				&& phoneStateListener != null)
		{
			tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
		}
		unregisterReceiver(appCloseReceiver);
		super.onTerminate();
	}




	
	
	public static final String GROUP_CREATE_RESULT_ACTION = "org.hytera.create_group_ACTION";
	public void creatProduct()
	{
		Log.d(THIS_FILE,  "creatProduct");
		if(product == null){
			this.product = new Product(this,ProductConst.ProductType_NAPP_RADIO,ProductConst.Product_Simple);
		}
		this.product.setLogStatus();
		this.product.setPCallClass(CallActivity.class, FloatWindowService.class);
		this.product.setGCallVideoClass(GroupCallVideoActivity.class);
		this.product.setNotifyListener(this);
		
		Intent createintent = new Intent(GROUP_CREATE_RESULT_ACTION);
		sendBroadcast(createintent);
	}
	
	public Product getProduct() {
		return product;
	}
	
	public void showWarning(String msg){
		Intent intent = new Intent();
		intent.setAction("android.intent.action.WARNING");
		intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		intent.putExtra("msg", msg);
		startActivity(intent);
	}


	@Override
	public void receiveNotify(String msg) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE, "BlackList application msg = "+msg);
		showWarning(msg);
	}
//	@Override
//	public void receiveOnlineMsg()
//	{
//		Log.d(THIS_FILE, "receiveOnlineMsg");
//		if(product != null
//			&& product.getOperator(ProductConst.Func_Addr) != null)
//		{
//			SysConfig config = product.getSysConfig();
//			AddrOperator addrOperator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
//			addrOperator.loadAddress(config,addrUptListener);
//		}
//	}

	public void setAddrUptListener(IHttpAddrOpCb addrUptListener) {
		this.addrUptListener = addrUptListener;
	}
}
